/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.mk.transaction.receiptproduction;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Sequence;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.ronsicecream.pos.entities.PosMasterItemStock;
import com.ronsicecream.pos.entities.PosMasterProcess;
import com.ronsicecream.pos.entities.PosMasterProcessDetail;
import com.ronsicecream.pos.entities.PosMasterStatus;
import com.ronsicecream.pos.entities.PosMasterUnit;
import com.ronsicecream.pos.entities.PosTransactionItemStock;
import com.ronsicecream.pos.entities.PosTransactionMkReceiptProduction;
import com.ronsicecream.pos.entities.PosTransactionMkReceiptProductionDetail;
import com.ronsicecream.pos.entities.PosTransactionMkWorkOrder;
import com.ronsicecream.pos.entities.PosTransactionMkWorkOrderDetail;
import com.ronsicecream.pos.master.item.Item;
import com.ronsicecream.pos.master.status.Status;
import com.ronsicecream.pos.mk.transaction.receiptproductiondetail.ReceiptProductionDetail;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class ReceiptProduction extends Page {

    private Field receiptId;
    private Field receiptDate;
    private Field receiptNumber;
    private Field receiptTotal;
    private Field receiptQuantity;
    private Field receiptPaid;
    private Field receiptUnpaid;
    private Field receiptKeel;
    private Field receiptDescription;
    private Field receiptTimestamp;
    private Field receiptRegionalId;
    private Field receiptPosUnitId;
    private Field receiptWorkOrderId;
    private Field receiptUserId;
    private Field receiptUnitId;

    public ReceiptProduction() {
        super();

        setTheClass(PosTransactionMkReceiptProduction.class);
        setPageDetail(ReceiptProductionDetail.class);

        receiptId = new Field();
        receiptId.setName("receiptId");
        receiptId.setCaption("id");
        receiptId.setListProperties("");
        receiptId.setInputProperties("");
        getFields().add(receiptId);

        receiptNumber = new Field();
        receiptNumber.setName("receiptNumber");
        receiptNumber.setCaption("number");
        receiptNumber.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptNumber.setInputProperties(" size=\"30\" readonly");
        receiptNumber.setRequired(true);
        getFields().add(receiptNumber);

        receiptPosUnitId = new Field();
        receiptPosUnitId.setName("receiptPosUnitId");
        receiptPosUnitId.setCaption("pos unit");
        receiptPosUnitId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptPosUnitId.setInputProperties("");
        getFields().add(receiptPosUnitId);

        receiptRegionalId = new Field();
        receiptRegionalId.setName("receiptRegionalId");
        receiptRegionalId.setCaption("regional");
        receiptRegionalId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptRegionalId.setInputProperties("");
        getFields().add(receiptRegionalId);

        receiptUnitId = new Field();
        receiptUnitId.setName("receiptUnitId");
        receiptUnitId.setCaption("unit");
        receiptUnitId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptUnitId.setInputProperties("");
        getFields().add(receiptUnitId);

        receiptUserId = new Field();
        receiptUserId.setName("receiptUserId");
        receiptUserId.setCaption("user");
        receiptUserId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptUserId.setInputProperties("");
        getFields().add(receiptUserId);

        receiptWorkOrderId = new Field();
        receiptWorkOrderId.setName("receiptWorkOrderId");
        receiptWorkOrderId.setCaption("spk");
        receiptWorkOrderId.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        receiptWorkOrderId.setInputProperties(" size=\"30\" readonly");
        receiptWorkOrderId.setRequired(true);
        receiptWorkOrderId.setSelectorLoad(true);
        receiptWorkOrderId.setSelectorCondition("t.workOrderUnreceived >0");
        receiptWorkOrderId.getSelectorColumns().add(new String[]{"workOrderNumber", "Number", "width=\"20%\" nowrap"});
        receiptWorkOrderId.getSelectorColumns().add(new String[]{"workOrderQuantity", "MainKitchen", "width=\"20%\""});
        receiptWorkOrderId.getSelectorColumns().add(new String[]{"workOrderTotal", " Total", " align=\"right\" width=\"10%\" nowrap"});
//        receiptWorkOrderId.getSelectorColumns().add(new String[]{"distributionUnsent", " Unsent", " align=\"right\" width=\"10%\" nowrap"});
        receiptWorkOrderId.getSelectorColumns().add(new String[]{"workOrderDescription", " Description", "width=\"40%\""});
        receiptWorkOrderId.setSelectorOrder("t.workOrderNumber");
        getFields().add(receiptWorkOrderId);

        receiptDate = new Field();
        receiptDate.setName("receiptDate");
        receiptDate.setCaption("date");
        receiptDate.setListProperties(" align=\"center\" width=\"6%\" nowrap");
        receiptDate.setInputProperties(" size=\"8\" readonly");
        receiptDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(receiptDate);

        receiptQuantity = new Field();
        receiptQuantity.setName("receiptQuantity");
        receiptQuantity.setCaption("Quantity");
        receiptQuantity.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        receiptQuantity.setInputProperties(" size=\"8\"");
        receiptQuantity.setRequired(true);
        getFields().add(receiptQuantity);

        receiptTotal = new Field();
        receiptTotal.setName("receiptTotal");
        receiptTotal.setCaption("total");
        receiptTotal.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        receiptTotal.setInputProperties(" size=\"8\"");
        receiptTotal.setRequired(true);
//        receiptTotal.setFormater(MoneyFormatter.getInstance());
        getFields().add(receiptTotal);

        receiptPaid = new Field();
        receiptPaid.setName("receiptPaid");
        receiptPaid.setCaption("paid");
        receiptPaid.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        receiptPaid.setInputProperties(" size=\"8\"");
        receiptPaid.setRequired(true);
//        receiptPaid.setFormater(MoneyFormatter.getInstance());
        getFields().add(receiptPaid);

        receiptUnpaid = new Field();
        receiptUnpaid.setName("receiptUnpaid");
        receiptUnpaid.setCaption("unpaid");
        receiptUnpaid.setListProperties(" align=\"right\" width=\"8%\" nowrap");
        receiptUnpaid.setInputProperties(" size=\"8\"");
        receiptUnpaid.setRequired(true);
//        receiptPaid.setFormater(MoneyFormatter.getInstance());
        getFields().add(receiptPaid);

        receiptKeel = new Field();
        receiptKeel.setName("receiptKeel");
        receiptKeel.setCaption("keel");
        receiptKeel.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        receiptKeel.setInputProperties(" ");
        receiptKeel.getOptions().add(new String[]{"1", "NO"});
        receiptKeel.getOptions().add(new String[]{"2", "YES"});
        getFields().add(receiptKeel);

        receiptTimestamp = new Field();
        receiptTimestamp.setName("receiptTimestamp");
        receiptTimestamp.setCaption("timestamp");
        receiptTimestamp.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        receiptTimestamp.setInputProperties(" size=\"12\"");
        getFields().add(receiptTimestamp);

        receiptDescription = new Field();
        receiptDescription.setName("receiptDescription");
        receiptDescription.setCaption("description");
        receiptDescription.setListProperties(" width=\"70%\"");
        receiptDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        receiptDescription.setInputType(Field.INPUT_TEXT_AREA);
        receiptDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(receiptDescription);

        getActions().add(Action.TRANS_ADD);
        getActions().add(Action.TRANS_EDIT);
        getActions().add(Action.TRANS_VIEW);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(receiptNumber);
        getFieldsAdd().add(receiptWorkOrderId);
//        getFieldsAdd().add(receiptRegionalId);
        getFieldsAdd().add(receiptDate);
        getFieldsAdd().add(receiptDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(receiptNumber);
        getFieldsEdit().add(receiptWorkOrderId);
//        getFieldsEdit().add(receiptRegionalId);
        getFieldsEdit().add(receiptDate);
        getFieldsEdit().add(receiptDescription);

        getFieldsList().clear();
        getFieldsList().add(receiptNumber);
        getFieldsList().add(receiptWorkOrderId);
        getFieldsList().add(receiptRegionalId);
        getFieldsList().add(receiptPosUnitId);
        getFieldsList().add(receiptDate);
        getFieldsList().add(receiptQuantity);
        getFieldsList().add(receiptTotal);
        getFieldsList().add(receiptPaid);
        getFieldsList().add(receiptKeel);
        getFieldsList().add(receiptDescription);

        getFieldsView().clear();
        getFieldsView().add(receiptDate);
        getFieldsView().add(receiptNumber);
        getFieldsView().add(receiptWorkOrderId);
        getFieldsView().add(receiptRegionalId);
        getFieldsView().add(receiptPosUnitId);
        getFieldsView().add(receiptQuantity);
        getFieldsView().add(receiptTotal);
        getFieldsView().add(receiptPaid);
        getFieldsView().add(receiptUnpaid);
        getFieldsView().add(receiptKeel);
        getFieldsView().add(receiptDescription);
        getFieldsView().add(receiptTimestamp);
        
        setExportList(getClass().getResourceAsStream("ReceiptList.jasper"));
        setExport(getClass().getResourceAsStream("Receipt.jasper"));

        setController(new ReceiptProductionController(this));
        getController().setOrder(receiptTimestamp);
        getController().setOrderType(Controller.ORDER_ASC);
    }

    class ReceiptProductionController extends Controller {

        public ReceiptProductionController(Page page) {
            super(page);
            setOrder(receiptTimestamp);
            setOrderType("ASC");
        }

        @Override
        public void beforeInsert(Object data, List list) {
            PosTransactionMkReceiptProduction receipt = (PosTransactionMkReceiptProduction) data;
            EntityManager em = getEntityManager();
            receipt.setReceiptNumber(generate());
            receipt.setReceiptUnitId(getCurrentUnit());
            receipt.setReceiptUserId(getCurrentUser());
            receipt.setReceiptTimestamp(Utility.getTimestamp());
            receipt.setReceiptKeel("1");
       for (Object detail : list) {
                System.out.println(" Jalan ga jos ");
                PosTransactionMkReceiptProductionDetail receiptDetail = (PosTransactionMkReceiptProductionDetail) detail;
                DateFormat df = new SimpleDateFormat("ddMMyyyy HH:mm:ss");
                long today = receiptDetail.getReceiptDetailDateExpired().getTime();
                String reportDate = df.format(today);
                System.out.println("Report Date: " + reportDate.substring(0, 4));
                System.out.println("Report Date dua: " + reportDate.substring(6, 8));
                receiptDetail.setReceiptDetailBarcode(reportDate.substring(0, 4) + reportDate.substring(6, 8) + generateBarcode());
                System.out.println(" jalan ga " + receiptDetail.getReceiptDetailBarcode());
            }


        }

        @Override
        public void afterInsert(Object data, List list) {
            EntityManager em = getEntityManager();
            PosMasterStatus statusGood = Status.getStatusGood(em);
            try {
                em.getTransaction().begin();
                PosTransactionMkReceiptProduction receipt = (PosTransactionMkReceiptProduction) data;
                PosTransactionMkWorkOrder spk = receipt.getReceiptWorkOrderId();
                receipt.setReceiptRegionalId(receipt.getReceiptWorkOrderId().getWorkOrderRegionalId());
                receipt.setReceiptPosUnitId(receipt.getReceiptWorkOrderId().getWorkOrderPosUnitId());

//                PosMasterUnit posunit = (PosMasterUnit) em.createQuery("SELECT t FROM PosMasterUnit t"
//                        + " WHERE t.unitUnitTypeId.unitTypeId=3 "
//                        + " AND t.unitUnitId.unitId=" + receipt.getReceiptUnitId().getUnitId()
//                        + " AND t.unitRegionalId.regionalId=" + receipt.getReceiptRegionalId().getRegionalId())
//                        .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                System.out.println("unittt :" + posunit + " unit name: " + posunit.getUnitName());

//                receipt.setReceiptPosUnitId(posunit);

//                //*****WH
//                PosMasterUnit posunitWarehouse = (PosMasterUnit) em.createQuery("SELECT t FROM PosMasterUnit t"
//                        + " WHERE t.unitUnitTypeId.unitTypeId=2 "
//                                                + " AND t.unitUnitId.unitId=" + receipt.g
//                        + " AND "
//                        + " t.unitRegionalId.regionalId=" + receipt.getReceiptRegionalId().getRegionalId())
//                        .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                System.out.println("unittt :" + posunitWarehouse + " unit name: " + posunitWarehouse.getUnitName());



                BigDecimal total = BigDecimal.ZERO;
                BigDecimal totalQuantity = BigDecimal.ZERO;
                for (Object detail : list) {
                    PosTransactionMkReceiptProductionDetail receiptDetail = (PosTransactionMkReceiptProductionDetail) detail;

                    PosTransactionMkWorkOrderDetail woDetail = (PosTransactionMkWorkOrderDetail) em.createQuery("SELECT t FROM PosTransactionMkWorkOrderDetail t "
                            + " WHERE t.workOrderDetailWorkOrderId.workOrderId='"
                            + spk.getWorkOrderId()
                            + "' AND t.workOrderDetailItemId.itemId ='"
                            + receiptDetail.getReceiptDetailItemId().getItemId()
//                            + "' AND t.purchaseOrderDetailSatuanId.satuanId='"
//                            + receiptDetail.getReceiptDetailSatuanId().getSatuanId() 
                            + "' ")
                            .setHint(Persistence.REFRESH_HINT, true).getSingleResult();

                    woDetail.setWorkOrderDetailSent(woDetail.getWorkOrderDetailSent().add(receiptDetail.getReceiptDetailQuantity()));
                    woDetail.setWorkOrderDetailUnsent(woDetail.getWorkOrderDetailUnsent().subtract(receiptDetail.getReceiptDetailQuantity()));

//                    List<PosTr


                    //*****Stok WH bertambah*****
                    PosMasterItemStock stockProd = Item.getStockItem(em, receiptDetail.getReceiptDetailItemId(), getCurrentUnit());
                    PosTransactionItemStock prodStock = new PosTransactionItemStock();

                    prodStock.setItemStockId(Sequence.next(prodStock));
                    prodStock.setItemStockItemId(receiptDetail.getReceiptDetailItemId());
                    prodStock.setItemStockUnitId(getCurrentUnit());
//                    prodStock.setItemStockStatusId(statusGood);
                    prodStock.setItemStockType("8");
//                    prodStock.setItemStockRegionalId(receipt.getReceiptRegionalId());
//                    prodStock.setItemStockPosUnitId(receipt.getReceiptPosUnitId());
                    prodStock.setItemStockLast(stockProd.getItemStockValue());
                    prodStock.setItemStockValue(receiptDetail.getReceiptDetailQuantity());
                    prodStock.setItemStockCurrent(stockProd.getItemStockValue().add(prodStock.getItemStockValue()));
                    prodStock.setItemStockUserId(getCurrentUser());
                    prodStock.setItemStockTimestamp(Utility.getTimestamp());
                    System.out.println(prodStock.getItemStockId() + "<<=========dedy=");
                    em.persist(prodStock);

                    stockProd.setItemStockValue(prodStock.getItemStockCurrent());
                    stockProd.setItemStockTimestamp(Utility.getTimestamp());

                    //*****Stok MK berkurang*****
                    List<PosMasterProcess> process;
                    process = em.createQuery("SELECT t FROM PosMasterProcess t WHERE"
                            + " t.processItemId.itemId=" + receiptDetail.getReceiptDetailItemId().getItemId())
                            .setHint(Persistence.REFRESH_HINT, true).getResultList();
                    System.out.println("Process 2:" + process);
                    if (process != null) {
                        for (PosMasterProcess pro : process) {
                            List<PosMasterProcessDetail> processDetail;
                            processDetail = em.createQuery("SELECT t FROM PosMasterProcessDetail t WHERE "
                                    + "t.processDetailProcessId.processId=" + pro.getProcessId()).setHint(Persistence.REFRESH_HINT, true).getResultList();
                            System.out.println("Cek Process Detail = " + processDetail);
                            if (processDetail != null) {
                                for (PosMasterProcessDetail proDetail : processDetail) {
                                    if (proDetail.getProcessDetailItemId().getItemTypeId().getTypeId() == 1) {
//                                             //*** STOCK MK MATERIAL BERKURANG**
                                                            PosMasterItemStock stock = Item.getStockItem(em, proDetail.getProcessDetailItemId(), getCurrentUnit());
                                                            PosTransactionItemStock itemStock = new PosTransactionItemStock();

                                                            itemStock.setItemStockId(Sequence.next(itemStock));
                                                            itemStock.setItemStockItemId(proDetail.getProcessDetailItemId());
                                                            itemStock.setItemStockUnitId(receipt.getReceiptUnitId());
//                    itemStock.setItemStockStatusId(statusGood);
                                                            itemStock.setItemStockType("8");
//                                                            itemStock.setItemStockRegionalId(receipt.getReceiptRegionalId());
//                                                            itemStock.setItemStockPosUnitId(receipt.getReceiptPosUnitId());
                                                            itemStock.setItemStockLast(stock.getItemStockValue());
                                                            itemStock.setItemStockValue(proDetail.getProcessDetailQuantity().multiply(receiptDetail.getReceiptDetailQuantity()));
                                                            itemStock.setItemStockCurrent(stock.getItemStockValue().subtract(itemStock.getItemStockValue()));
                                                            itemStock.setItemStockUserId(getCurrentUser());
                                                            itemStock.setItemStockTimestamp(Utility.getTimestamp());
                                                            System.out.println(itemStock.getItemStockId() + "<<=========dedy=");
                                                            em.persist(itemStock);

                                                            stock.setItemStockValue(itemStock.getItemStockCurrent());
                                                            stock.setItemStockTimestamp(Utility.getTimestamp());
                                    } else {
                                        List<PosMasterProcess> process2;
                                        process2 = em.createQuery("SELECT t FROM PosMasterProcess t WHERE"
                                                + " t.processItemId.itemId=" + proDetail.getProcessDetailItemId().getItemId())
                                                .setHint(Persistence.REFRESH_HINT, true).getResultList();
                                        System.out.println("Process 2:" + process2);
                                        if (process2 != null) {
                                            for (PosMasterProcess proc2 : process2) {
                                                List<PosMasterProcessDetail> processDetail2;
                                                processDetail2 = em.createQuery("SELECT t FROM PosMasterProcessDetail t WHERE "
                                                        + "t.processDetailProcessId.processId=" + proc2.getProcessId()).setHint(Persistence.REFRESH_HINT, true).getResultList();
                                                System.out.println("Cek Process Detail 2= " + processDetail2);
                                                if (processDetail2 != null) {
                                                    for (PosMasterProcessDetail proDetail2 : processDetail2) {
                                                        if (proDetail2.getProcessDetailItemId().getItemTypeId().getTypeId() == 1) {
                                                            
                                                            //*** STOCK MK MATERIAL BERKURANG**
                                                            PosMasterItemStock stock = Item.getStockItem(em, proDetail2.getProcessDetailItemId(), getCurrentUnit());
                                                            PosTransactionItemStock itemStock = new PosTransactionItemStock();

                                                            itemStock.setItemStockId(Sequence.next(itemStock));
                                                            itemStock.setItemStockItemId(proDetail2.getProcessDetailItemId());
                                                            itemStock.setItemStockUnitId(receipt.getReceiptUnitId());
//                    itemStock.setItemStockStatusId(statusGood);
                                                            itemStock.setItemStockType("8");
//                                                            itemStock.setItemStockRegionalId(receipt.getReceiptRegionalId());
//                                                            itemStock.setItemStockPosUnitId(receipt.getReceiptPosUnitId());
                                                            itemStock.setItemStockLast(stock.getItemStockValue());
                                                            itemStock.setItemStockValue(proDetail2.getProcessDetailQuantity().multiply(receiptDetail.getReceiptDetailQuantity()));
                                                            itemStock.setItemStockCurrent(stock.getItemStockValue().subtract(itemStock.getItemStockValue()));
                                                            itemStock.setItemStockUserId(getCurrentUser());
                                                            itemStock.setItemStockTimestamp(Utility.getTimestamp());
                                                            System.out.println(itemStock.getItemStockId() + "<<=========dedy=");
                                                            em.persist(itemStock);

                                                            stock.setItemStockValue(itemStock.getItemStockCurrent());
                                                            stock.setItemStockTimestamp(Utility.getTimestamp());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    total = total.add(receiptDetail.getReceiptDetailPrice().multiply(receiptDetail.getReceiptDetailQuantity()));
                    totalQuantity = totalQuantity.add(receiptDetail.getReceiptDetailQuantity());
                }
                System.out.println("totalQuantity " + totalQuantity);
//                receipt.getReceiptDistributionId().setDistributionSent(receipt.getReceiptDistributionId().getDistributionSent().add(totalQuantity));
//                receipt.getReceiptDistributionId().setDistributionUnsent(receipt.getReceiptDistributionId().getDistributionUnsent().subtract(totalQuantity));
                spk.setWorkOrderReceived(spk.getWorkOrderReceived().add(totalQuantity));
                spk.setWorkOrderUnreceived(spk.getWorkOrderUnreceived().subtract(totalQuantity));
                receipt.setReceiptTotal(total);
                receipt.setReceiptUnpaid(total);
                receipt.setReceiptPaid(BigDecimal.ZERO);
                receipt.setReceiptQuantity(totalQuantity);
                em.getTransaction().commit();
            } catch (Exception e) {
                Logger.getInstance().log(e);
                System.out.println("eeeeeeerrr " + e);
            }

        }
    }

    
    
    
    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        receiptDate.setFormater(DateFormatter.getInstanceDate(this));
        receiptTimestamp.setFormater(DateFormatter.getInstanceDateTime(this));
    }

    @Override
    public void beforeAdd() {
        receiptNumber.setDefaultValue(generate());
    }

    public String generate() {
        String unitCode = (getCurrentUnit()).getUnitCode();
        Calendar cal = Calendar.getInstance();
        String year = "" + cal.get(Calendar.YEAR);
        String month = Utility.intToRoman(cal.get(Calendar.MONTH) + 1);
        try {
            PosTransactionMkReceiptProduction rec = (PosTransactionMkReceiptProduction) getEntityManager().createQuery("SELECT t FROM PosTransactionMkReceiptProduction t "
                    + "WHERE t.receiptNumber LIKE '%/" + unitCode + "/Rons/REC-WH/%/" + year + "' "
                    + "ORDER BY t.receiptTimestamp DESC").setHint(Persistence.REFRESH_HINT, true).setFirstResult(0).setMaxResults(1).getSingleResult();
            return Utility.lpad(Integer.parseInt(rec.getReceiptNumber().substring(0, 4)) + 1, 4, "0") + "/" + unitCode + "/Rons/REC-WH/" + month + "/" + year;
        } catch (Exception e) {
        }
        return "0001/" + unitCode + "/Rons/REC-WH/" + month + "/" + year;
    }
    
     private String generateBarcode() {

            Random random = new Random();
            String bcode = "" + (random.nextInt(899999) + 100000);
            System.out.println("jose barcode" + bcode);
            String barcode;
            if (bcode.length() == 1) {
                barcode = "00000" + bcode;
            } else if (bcode.length() == 2) {
                barcode = "0000" + bcode;
            } else if (bcode.length() == 3) {
                barcode = "000" + bcode;
            } else if (bcode.length() == 4) {
                barcode = "00" + bcode;
            } else if (bcode.length() == 5) {
                barcode = "0" + bcode;
            } else {
                barcode = bcode;
            }
            return barcode;
        }

        private String cekBarcode() {
            EntityManager em = getEntityManager();
            String barcode = generateBarcode();
            try {
                Query query = em.createQuery("SELECT t PosTransactionMkReceiptProductionDetail t WHERE t.receiptDetailBarcode =" + generateBarcode());
                List<PosTransactionMkReceiptProductionDetail> list = query.getResultList();
                System.out.println("" + list);
                if (list.size() > 0) {
                    exist();
                } else if ("0000000000".equals(barcode)) {
                    exist();
                } else {
                    return barcode;
                }
            } catch (Exception e) {
                Logger.getInstance().log(e);
            }
            return barcode;
        }

        public void exist() {
            cekBarcode();
        }
    }

